// pages/editAddress/editAddress.js
var config = require("../../utils/config.js");
var network = require("../../utils/network.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['陕西省', '西安市', ''],
    customItem: '全部',
    name:'',
    tel:'',
    address:'',
    m:'',
    id:'', //客户id
    isMo:0,  //是否这是默认
    addressId:'',//地址id
    checked:false,
    isShowDel:false  //是否展示删除键
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.data){
      console.log(111)
      let data=JSON.parse(options.data)
      let list = []
      list.push(data.province)
      list.push(data.city)
      list.push(data.area)
      this.setData({
        name:data.name,
        tel:data.phone,
        address:data.address,
        region:list,
        addressId:data.id,
        m:data.m,
        isShowDel:true
      })
      if(data.is_default==1){
        this.setData({
          isMo:'1',
          checked:true
        })
      }
    }
    this.setData({
      id:wx.getStorageSync("userInfo").id
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  bindNameInput:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  bindTelInput: function (e) {
    this.setData({
      tel: e.detail.value
    })
  },
  bindAddressInput: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  bindmInput: function (e) {
    this.setData({
      m: e.detail.value
    })
  },
  switch1Change(e) {
    console.log('switch1 发生 change 事件，携带值为', e.detail.value)
    if(e.detail.value){
      this.setData({
        isMo:'1',
        checked:true
      })
    }else{
      this.setData({
        isMo: '0',
        checked:false
      })
    }
  },
  postAddress:function(){
    if (this.data.isShowDel){
      network.request({
        url: config.host + 'api_address/addressEdit',
        method: "POST",
        data: {
          region: this.data.region,
          name: this.data.name,
          tel: this.data.tel,
          address: this.data.address,
          m: this.data.m,
          isMo: this.data.isMo,
          id: this.data.addressId,
          customer_id: this.data.id
        },
        success: res => {
          if (res.data.code == 99) {
            wx.showToast({
              title: res.data.msg,
            })
            this.goBack();
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }else{
      network.request({
        url: config.host +'api_address/address',
        method:"POST",
        data:{
          id:this.data.id,
          region: this.data.region,
          name:this.data.name,
          tel:this.data.tel,
          address:this.data.address,
          m:this.data.m,
          isMo:this.data.isMo
        },
        success:res=>{
          if(res.data.code==99){
            wx.showToast({
              title:res.data.msg,
            })
            this.goBack();
          }else{
            wx.showToast({
              title:res.data.msg,
              icon:'none'
            })
          }
        }
      })
    }
  },
  // 删除地址
  delAddress(){
    wx.showModal({
      title: '确认删除地址',
      content: '地址删除',
      success: res => {
        if (res.confirm) {
          network.request({
            url: config.host + 'api_address/addressDelete',
            method: "POST",
            data: {
              id: this.data.addressId,
            },
            success: res => {
              if (res.data.code == 99) {
                this.goBack();
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            }
          })
        } else {
          
        }
      }
    })
   
  },
  // 返回上一个并刷新
  goBack(){
    var pages = getCurrentPages();//获取页面栈
    console.log(pages)
    if (pages.length > 1) {
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      //调用上一个页面的onShow方法
      prePage.getList()
    }
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})